﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.DAL;
using WXYW.BLL;

namespace WXYW
{
    public partial class ProblemReportEditReport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ProblemReportService service = new ProblemReportService();
                List<ReportType> reportTypes = new List<ReportType>();
                reportTypes.AddRange(service.GetReportTypeList().ToList());
                reportTypeDDL.DataSource = reportTypes;
                reportTypeDDL.DataBind();

                List<ProductLineType> productLineTypes = new List<ProductLineType>();
                productLineTypes.AddRange(service.GetProductLineTypeList().ToList());
                productLineTypeDDL.DataSource = productLineTypes;
                productLineTypeDDL.DataBind();

                ProblemTypeService problemTypeService = new ProblemTypeService();
                List<ProblemType> problemTypes = new List<ProblemType>();
                problemTypes.AddRange(problemTypeService.GetAllProblemType());

                ProductClassService productClassService = new ProductClassService();
                List<ProductVersion> productVersions = new List<ProductVersion>();
                productVersions.AddRange(productClassService.GetVersions().ToList());
                productVersionDDL.DataSource = productVersions;
                productVersionDDL.DataBind();

                ProblemReportService reportService = new ProblemReportService();
                ProblemReport reportEntity = reportService.GetProblemReport(Request.QueryString["Code"]);
                reportTypeDDL.SelectedValue = reportEntity.ReportType;
                productLineTypeDDL.SelectedValue = reportEntity.ProductLineType;
                productVersionDDL.SelectedValue = reportEntity.ProductVersion;
                if (!reportEntity.UserReference.IsLoaded)
                {
                    reportEntity.UserReference.Load();
                }
                createByLabel.Text = reportEntity.User.ContactPerson;
                createDateLabel.Text = reportEntity.CreateDate.ToString("yyyy-MM-dd");
                productModelTxtBox.Text = reportEntity.ProductType;
                solutionTxtBox.Text = reportEntity.Solution;
                subjectTxtBox.Text = reportEntity.Subject;
                if (reportEntity.SecurityType.Equals("ASC"))
                {
                    SecurityRadioButtonList.SelectedIndex = 1;
                }
            }
        }

        protected void problemFileLnkBtn_Click(object sender, EventArgs e)
        {
            ReportFileModalPopupExtender.Show();
        }

        protected void okLinkButton_Click(object sender, EventArgs e)
        {
            string errorMsg = string.Empty;
            if (string.IsNullOrEmpty(productModelTxtBox.Text.Trim()))
            {
                errorMsg += "请填写产品型号;";
            }
            if (string.IsNullOrEmpty(subjectTxtBox.Text.Trim()))
            {
                errorMsg += "请填写资料报告主题;";
            }

            if (!string.IsNullOrEmpty(errorMsg))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "tip", string.Format("alert('错误提示:{0}');", errorMsg), true);
            }
            else
            {
                ProblemReportService problemReportService = new ProblemReportService();
                ProblemReport problemReportEntity = problemReportService.GetProblemReport(Request.QueryString["Code"]);
                problemReportEntity.ProductLineType = productLineTypeDDL.SelectedItem.Text;
                problemReportEntity.ProductType = productModelTxtBox.Text.Trim();
                problemReportEntity.ProductVersion = productVersionDDL.SelectedItem.Text;
                problemReportEntity.ReportType = reportTypeDDL.SelectedItem.Text;
                problemReportEntity.SecurityType = SecurityRadioButtonList.SelectedValue;
                problemReportEntity.Solution = solutionTxtBox.Text.Trim();
                problemReportEntity.Subject = subjectTxtBox.Text.Trim();
                problemReportService.UpdateProblemReport(problemReportEntity);
                Response.Write(string.Format("<script language='JavaScript'> alert( '资料报告已成功更新！');location.href= 'ProblemReportManage.aspx?T={0}&P={1}' </script> ", Request.QueryString["T"], Request.QueryString["P"]));
            }
        }

        protected void backLinkButton_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("ProblemReportManage.aspx?T={0}&P={1}", Request.QueryString["T"], Request.QueryString["P"]));
        }

        protected void ReportFileGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
                ((HyperLink)e.Row.FindControl("ViewFileHyperLink")).NavigateUrl = string.Format("~/ReportFile/{0}_{1}", ((ProblemReportFile)e.Row.DataItem).ID, ((ProblemReportFile)e.Row.DataItem).FileName);
            }
        }

        protected void ModalPopupExtender1_Load(object sender, EventArgs e)
        {
            ProblemReportService reportService = new ProblemReportService();
            IQueryable<ProblemReportFile> files = reportService.GetReportFiles(Request.QueryString["Code"]);
            ReportFileGridView.DataSource = files;
            ReportFileGridView.DataBind();
        }

        protected void uploadReportFileBtn_Click(object sender, EventArgs e)
        {
            if (ReportFileFileUpload.HasFile)
            {
                ProblemReportService reportService = new ProblemReportService();
                long fileID = reportService.AddProblemReportFile(Request.QueryString["Code"], ReportFileFileUpload.FileName);
                ReportFileFileUpload.SaveAs(Server.MapPath(string.Format("ReportFile/{0}_{1}", fileID, ReportFileFileUpload.FileName)));

                IQueryable<ProblemReportFile> files = reportService.GetReportFiles(Request.QueryString["Code"]);
                ReportFileGridView.DataSource = files;
                ReportFileGridView.DataBind();

                ReportFileModalPopupExtender.Show();
            }
        }

        protected void deleteFileLnBtn_click(object sender, EventArgs e)
        {
            LinkButton deleteFileLnkBtn = sender as LinkButton;
            ProblemReportService reportService = new ProblemReportService();
            reportService.DeleteReportFile(Convert.ToInt32(deleteFileLnkBtn.CommandArgument));

            IQueryable<ProblemReportFile> files = reportService.GetReportFiles(Request.QueryString["Code"]);
            ReportFileGridView.DataSource = files;
            ReportFileGridView.DataBind();

            ReportFileModalPopupExtender.Show();
        }
    }
}
